package com.hzjh.base.style.view.round

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.annotation.ColorInt
import androidx.constraintlayout.widget.ConstraintLayout

class HollowOutGuideConstraintLayout
@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    ConstraintLayout(context, attrs, defStyleAttr) {
    private var mPaintColor = Color.parseColor("#520F172A")
    private var mAnchorRect: RectF? = null
    private var allRadius: Float = 0f
    private val mPaint: Paint by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = mPaintColor
        }
    }

    fun setRect(
        anchorRect: RectF,
        allR: Float = 0f,
        @ColorInt paintColor: Int = Color.parseColor("#b30F172A")
    ) {
        mAnchorRect = anchorRect
        allRadius = allR
        mPaintColor = paintColor
        invalidate()
    }

    //https://www.zhangshengrong.com/p/ArXGbWJENj/
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas?.let {
            mAnchorRect?.let {
                var path = Path()
                //https://www.jianshu.com/p/d1b5a7fe4832  求交集  背景设为透明 画笔设为灰色背景
                //https://blog.csdn.net/weixin_37618354/article/details/84842104  这个也行 但是没用这种方式
                path.fillType = Path.FillType.EVEN_ODD
                path.addRect(RectF(0f, 0f, width.toFloat(), height.toFloat()), Path.Direction.CW)
//                path.addOval(it, Path.Direction.CW)
                path.addRoundRect(it, allRadius, allRadius, Path.Direction.CW)
                canvas.drawPath(path, mPaint)
            }
        }
    }
}
